use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::backend::Backend;
use crate::config::backend::BackendConf;
use crate::error::*;

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct HttpBackendConf {
    // global_backend_name不存在时才使用backend配置.
    pub global_backend_name: Option<String>,
    pub backend_conf: Option<BackendConf>,
}

#[allow(dead_code)]
#[derive(Default)]
pub struct HttpBackendRuntimeConf {
    pub global_backend_name: Option<String>,
    pub backend: Option<Backend>,
}

#[allow(dead_code)]
impl HttpBackendConf {
    pub fn to_runtime_conf(&self) -> Result<Arc<HttpBackendRuntimeConf>> {
        let mut backend_runtime_conf = HttpBackendRuntimeConf {
            global_backend_name: self.global_backend_name.clone(),
            backend: None,
        };

        if let Some(backend_conf) = self.backend_conf.clone() {
            backend_runtime_conf.backend =
                Some(Backend::build(Arc::new(backend_conf))?);
        }

        Ok(Arc::new(backend_runtime_conf))
    }
}

#[allow(dead_code)]
impl HttpBackendRuntimeConf {
    fn valid(self) -> Result<Self> {
        Ok(self)
    }
}
